package cn.quevo.core.lang;

import cn.quevo.core.enums.ResultStatusEnum;
import cn.quevo.core.exception.CheckParamsException;

/**
 * Boolean 工具类
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018-03-09 16:00:52
 * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
 * @return
 */
public class BooleanUtils extends org.apache.commons.lang3.BooleanUtils {
    public static final String TRUE = "Y";
    public static final String FALSE = "N";
    public static final String TRUE_SINGLE = "T";
    public static final String FALSE_SINGLE = "F";

    /**
     * 验证bool是否为true
     *
     * @param bool
     * @return
     * @author July july_sky@foxmail.com
     * @date 2016年10月19日下午1:14:30
     */
    public static boolean exists(String bool) {
        return TRUE.equals(bool) || FALSE.equals(bool) || TRUE_SINGLE.equals(bool) || FALSE_SINGLE.equals(bool);
    }

    /**
     * 判断是否为true
     *
     * @param bool
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:01:36
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean isTrue(String bool) {
        return !StringUtils.isBlank(bool) && (bool.equals(TRUE) || bool.equals("TRUE") || bool.equals(TRUE_SINGLE));
    }

    /**
     * 格式化，如果不存在则直接为N
     *
     * @param bool
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:02:38
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static String format(String bool) {
        return isTrue(bool) ? "Y" : "N";
    }

    /**
     * 如果存在则格式化，否则抛出异常
     *
     * @param bool
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:02:48
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static String existsAndFormat(String bool) throws CheckParamsException {
        if (!exists(bool)) {
            throw new CheckParamsException(ResultStatusEnum.PARAMS_ERROR);
        }
        return format(bool);
    }

    /**
     * 将Boolean封装类型转换为值类型
     *
     * @param bool
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:03:32
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean valueOf(Boolean bool) {
        return bool != null && bool.booleanValue();
    }
}
